<?php
  error_reporting(E_ALL ^ E_DEPRECATED);

  mysql_connect('localhost', 'root', '');
  @mysql_select_db('phpbb') or die( "Unable to select database");

  $query = "SELECT * FROM `favl_pilotos` ORDER BY `nro_licencia`";
  $result = mysql_query($query);

  if (!$result) {
      echo "Could not successfully run query ($sql) from DB: " . mysql_error();
      exit;
  }

  if (mysql_num_rows($result) == 0) {
      echo "No rows found, nothing to print so am exiting";
      exit;
  }

  $entries = array();

  while ($row = mysql_fetch_assoc($result)) {
      $entries[] = array(
        'license' => $row['nro_licencia'],
        'name' => $row['nombre'],
        'logo_path' => $row['foto'],
        'license_level' => $row['nivel'],
        'address_line_1' => $row['domicilio'],
        'postal_code' => $row['cp'],
        'address_line_2' => $row['localidad'],
        'location' => $row['provincia'],
        'country_code' => $row['pais'],
        'dob' => $row['fecha_nac'],
        'nac' => $row['dni'],
        'email_primary' => $row['email'],
        'phone_work' => $row['telefono'],
        'phone_mobile' => $row['celular'],
        'tandem_license' => $row['biplaza'],
        'instructor_license' => $row['instructor'],
        'examiner_license' => $row['examinador'],
        'club' => $row['club'],
        'active' => $row['activo'],
        'gender' => $row['sexo'],
        'civl_id' => $row['id_civl'],
        'license_expiration' => $row['anio_venc_lic'],
        'pilot_type' => $row['tipo_piloto'],
        'gear' => $row['equipo'],
        'gear_type' => $row['tipo_equipo'],
        'comments' => $row['comentarios'],
        'license_fai' => $row['fai'],
        'license_fai_expiration' => $row['fai_vence'],
        'license_ippi' => $row['ippi'],
        'sponsor' => $row['sponsor'],
      );
  }

  mysql_free_result($result);

  require_once('lib/nusoap.php');
  $wsdlURL = "http://civlrankings.fai.org/FL.asmx?wsdl";
  $soap = new nusoap_client($wsdlURL, "wsdl");

  $i = 0;

  foreach ($entries as $key => $value) {
    $civl_name = $value['name'];
    echo "Looking for $civl_name\n";

    $parameters['parameters']['name'] = $civl_name;
    $result = $soap->call("FindPilot", $parameters);

    if(!is_array($result['FindPilotResult']['Person'])) {
      echo "No results found for $civl_name\n";
    }
    else { 
      $i=0;
      $person = $result['FindPilotResult']['Person'];
      $opt = 0;

      foreach ($person as $r) {
        if ($r['CodeISO'] == 'ARG') {
          echo ($opt++).") ".$r['Name'].' - '.$r['CIVLID']."\n";
          //var_dump($r);
        }
      }

      $handle = fopen ("php://stdin","r");
      $line = trim(fgets($handle));

      if($line == 'q') {
          echo "Ending...\n";
          exit;
      }

      if ($line == 'n') {
        echo "Next...\n";
        continue;
      }

      $selected = 0;

      while (($selected = ($line + 0)) >= $opt) {
        echo "invalid option, try again...\n";
      }

      echo "Selected = ".$result['FindPilotResult']['Person'][$selected]['Name']."\n";
      var_dump($value);
    }
  }
  mysql_close();
?>
